package com.ruoyi.common.business.service.impl;

import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.business.entity.YytTeamAssociation;
import com.ruoyi.common.business.entity.YytTeamDonateLog;
import com.ruoyi.common.business.dao.YytTeamDonateLogMapper;
import com.ruoyi.common.business.service.YytMemberService;
import com.ruoyi.common.business.service.YytTeamAssociationService;
import com.ruoyi.common.business.service.YytTeamDonateLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.oms.service.YytOmsOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
* <p>
    *  服务实现类
    * </p>
*
* @author MrPing
* @since 2022-09-06
*/
@Service
public class YytTeamDonateLogServiceImpl extends ServiceImpl<YytTeamDonateLogMapper, YytTeamDonateLog> implements YytTeamDonateLogService {

    @Autowired
    private YytOmsOrderService yytOmsOrderService;

    @Autowired
    private YytMemberService yytMemberService;

    @Autowired
    private YytTeamAssociationService yytTeamAssociationService;

    @Autowired
    private YytTeamDonateLogMapper yytTeamDonateLogMapper;


    @Override
    public void addTeamOrder(String orderId) {
        YytOmsOrder omsOrder = yytOmsOrderService.getById(orderId);
        if(StringUtils.isNotBlank(omsOrder.getTeamId())) {
            YytMember member = yytMemberService.getById(omsOrder.getMemberId());
            YytTeamAssociation association = yytTeamAssociationService.getById(omsOrder.getTeamId());
            YytTeamDonateLog donateLog = new YytTeamDonateLog();
            donateLog.setOrderId(omsOrder.getId());
            donateLog.setTeamId(omsOrder.getTeamId());
            donateLog.setTeamName(association.getName());
            donateLog.setMemberId(omsOrder.getMemberId());
            donateLog.setMemberName(member.getNickname());
            donateLog.setOrderMoney(omsOrder.getPayAmount());
            donateLog.setCreateTime(LocalDateTime.now());
            yytTeamDonateLogMapper.insert(donateLog);
            association.setMoney(association.getMoney().add(omsOrder.getPayAmount().multiply(new BigDecimal("0.1"))));
            yytTeamAssociationService.updateById(association);
        }
    }


}
